package org.millioncall.yueyoga.admin.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.millioncall.openplatform.common.PaginateParams;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.common.StartLimitPairImpl;
import org.millioncall.openplatform.dao.PreparedQuery;
import org.millioncall.yueyoga.admin.dao.BaseDao;
import org.millioncall.yueyoga.admin.dao.GroupDAO;
import org.millioncall.yueyoga.admin.dao.UserDAO;
import org.millioncall.yueyoga.admin.model.GroupUserModel;
import org.millioncall.yueyoga.admin.model.Hall;
import org.millioncall.yueyoga.admin.model.User;
import org.millioncall.yueyoga.admin.model.UserHallModel;
import org.millioncall.yueyoga.admin.model.UserHallRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class UserDAOImpl extends BaseDao implements UserDAO {
	@Autowired
	private GroupDAO groupDAO;

	@Override
	public User findById(String id) {
		return this.getPagableSqlSessionTemplate()
				.selectOne("findUserById", id);
	}

	@Override
	public int updatePassword(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().update("updatePassword", map);
	}
	
	@Override
	public int deleteUserById(List<String> ids) {
		return this.getPagableSqlSessionTemplate().delete("deleteUserById",
				ids);
	}
	
	@Override
	public User findByUsername(String username) {
		User user = null;
		user = this.getPagableSqlSessionTemplate().selectOne(
				"findUserByUserName", username);
		return user;
	}

	@Override
	public int save(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().insert("saveUser", map);
	}

	@Override
	public int update(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().update("updateUser", map);
	}

	@Override
	public int deleteByIds(String[] ids) {
		int rtn = 0;
		for (String id : ids) {
			rtn += delete(id);
		}
		return rtn;
	}

	@Override
	public int delete(String id) {
		return this.getPagableSqlSessionTemplate().update("deleteUserById", id);
	}

	@Override
	public List<User> findAllUsers(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().selectList("findAllUsers",map);
	}

	@Override
	public PaginatedList<User> findUsersInPage(int start, int limit,
			Map<String, Object> map) {
		PreparedQuery<User> pq = this.getPagableSqlSessionTemplate().findAll(
				"findUsersInPage", map);
		PaginatedList<User> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(start, limit)));
		return pl;
	}

	@Override
	public List<User> findByProperty(String key, String value) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("key", key);
		map.put("value", value);
		return this.getPagableSqlSessionTemplate().selectList(
				"findUserByProperty", map);
	}

	public GroupDAO getGroupDAO() {
		return groupDAO;
	}

	public void setGroupDAO(GroupDAO groupDAO) {
		this.groupDAO = groupDAO;
	}

	@Override
	public int updateUser(Map<String, Object> map) {
		return this.update("updateUser", map);
	}

	@Override
	public int updateUserGroups(Map<String, Object> map) {
		return this.update("updateUserGroups", map);
	}
	
	@Override
	public PaginatedList<User> findByGroupInPage(Integer start, Integer limit,
			String name) {
		PreparedQuery<User> pq = this.getPagableSqlSessionTemplate().findAll(
				"findUserByGroup", name);
		PaginatedList<User> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(start, limit)));
		return pl;
	}

	@Override
	public int deleteByGroup(String name) {
		return this.getPagableSqlSessionTemplate()
				.delete("deleteByGroup", name);
	}

	@Override
	public List<User> findByGroup(Integer start, Integer limit, String name) {
		PreparedQuery<User> pq = this.getPagableSqlSessionTemplate().findAll(
				"findUserByGroup", name);
		List<User> list = pq.list();
		return list;
	}

	@Override
	public List<User> findAllAssignedTo(String name) {
		PreparedQuery<User> pq = this.getPagableSqlSessionTemplate().findAll(
				"findAllAssignedTo", name);
		List<User> list = pq.list();
		return list;
	}

	public List<User> findTaskUser() {
		PreparedQuery<User> pq = this.getPagableSqlSessionTemplate().findAll(
				"findTaskUser");
		List<User> list = pq.list();
		return list;
	}

	@Override
	public int saveUserGroup(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().insert("saveUserGroup", map);
	}

	@Override
	public List<GroupUserModel> findGroupUserByGroupId(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().selectList(
				"findOfficerByGroupId", map);
	}

	@Override
	public List<UserHallModel> findUserGroupHall(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().selectList("findUserGroupHall",map);
	}

	@Override
	public Hall findUserByHallName(String hallcode) {
		return this.getPagableSqlSessionTemplate().selectOne("findUserByHallName",hallcode);
	}

	@Override
	public List<UserHallRole> findRoleId(String userId) {
		return this.getPagableSqlSessionTemplate().selectList("findRoleId",userId);
	}
}
